//
//  oldMutileIconScrollView.m
//  laonianbangfamily
//
//  Created by 勇拓 李 on 18/3/23.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldMutileIconScrollView.h"
#import "UIImageView+UIImageView_oldScrollView.h"
#import "oldSingleServiceIconView.h"
#import "oldHub.h"
#import "oldNetWork.h"

@interface oldMutileIconScrollView()<UIScrollViewDelegate, oldSingleServiceIconViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView1;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView2;
@property (weak, nonatomic) IBOutlet UIView *View1;
@property (weak, nonatomic) IBOutlet UIView *View2;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *img_arrow_lead;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *firstServiceView_width;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *secondServiceView_width;
@property (nonatomic, strong)UIPageControl *pageControl1;

@property (nonatomic, copy)NSArray *serviceArray;
@end

@implementation oldMutileIconScrollView

- (instancetype)init
{
    self = [[[NSBundle mainBundle] loadNibNamed:@"oldMutileIconScrollView" owner:self options:nil] objectAtIndex:0];
    if (self) {
        [self initScrollView];
        [self initData];
    }
    
    return self;
    
}

#pragma mark - initScrollView

- (void)initScrollView
{
    _img_arrow_lead.constant = (SCREEN_WIDTH/4 - 17)/2;
    [self layoutIfNeeded];
    
    _scrollView1.delegate = self;
    _scrollView1.pagingEnabled = YES;
    [_scrollView1 setShowsVerticalScrollIndicator:NO];
    [_scrollView1 setShowsHorizontalScrollIndicator:NO];
    
    _scrollView2.delegate = self;
    [_scrollView2 setShowsVerticalScrollIndicator:NO];
    _scrollView2.tag = noDisableHorizontalScrollTag;
}

#pragma mark - initData
- (void)initData
{
    WeakSelf
    
    _scrollView1.hidden = YES;
    _scrollView2.hidden = YES;
    [oldHub waitingWindow:@"加载服务信息中.."];
    
    [OLDNetWork httpRequestWithPostPut:URL_GetAllServeCategory params:@{@"pid":@"0"} success:^(id data) {
        [oldHub hideHUDWindow];
        if ([data count]) {
            [weakSelf initServicesArray:data];
        }
        weakSelf.scrollView1.hidden = NO;
        weakSelf.scrollView2.hidden = NO;
    } error:^(NSString *error) {
        [oldHub warningWindow:error];
    }];
}

- (void)initServicesArray:(id)data
{
    NSMutableArray *array = [NSMutableArray array];
    
    for (int i = 0; i < [data count]; i++) {
        oldService *service = [[oldService alloc] initWithDictionary:data[i]];
        [array addObject:service];
    }
    
    _serviceArray = array;
    
    [self initTopView];
}


- (void)initTopView
{
    _firstServiceView_width.constant = SCREEN_WIDTH/4 * [_serviceArray count];
    [_View1 layoutIfNeeded];
    
    for (int i = 0; i < [_serviceArray count]; i++) {
        oldSingleServiceIconView *view = [[[NSBundle mainBundle] loadNibNamed:@"oldSingleServiceIconView" owner:self options:nil] objectAtIndex:0];
        view.service = _serviceArray[i];
        view.delegate = self;
        view.index = i;
        view.frame = CGRectMake(SCREEN_WIDTH/4 * i, 0, SCREEN_WIDTH/4, 69);
        [_View1 addSubview:view];
    }
    
    [self initControl1];
    
    if ([_serviceArray count]) {
        oldService *service = _serviceArray[0];
        [self initSubTopView:service.childList];
    }
    
    NSLog(@"frame is %@", NSStringFromCGRect(_View1.frame));
}

- (void)initSubTopView:(NSArray *)array
{
    for (UIView *subView in _View2.subviews) {
        if ([subView isKindOfClass:[oldSingleServiceIconView class]]) {
            [subView removeFromSuperview];
        }
    }
    
    _secondServiceView_width.constant = SCREEN_WIDTH/4 * [array count];
    [_View2 layoutIfNeeded];
    
    for (int i = 0; i < [array count]; i++) {
        oldSingleServiceIconView *view = [[[NSBundle mainBundle] loadNibNamed:@"oldSingleServiceIconView" owner:self options:nil] objectAtIndex:0];
        view.service = array[i];
        view.delegate = self;
        view.frame = CGRectMake(SCREEN_WIDTH/4 * i, 0, SCREEN_WIDTH/4, 69);
        [_View2 addSubview:view];
    }
    
    [_scrollView2 flashScrollIndicators];
}

- (void)initControl1
{
    _pageControl1 = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 55 , SCREEN_WIDTH, 40)];
    [_pageControl1 setCurrentPage:0];
    _pageControl1.numberOfPages = [_serviceArray count]/4 + 1;//指定页面个数
    [_pageControl1 setBackgroundColor:[UIColor clearColor]];
    _pageControl1.currentPageIndicatorTintColor = OLD_COLOR;
    _pageControl1.pageIndicatorTintColor = Color_With_Rgb(229, 229, 229, 1);
    _pageControl1.userInteractionEnabled = NO;
    [self addSubview:_pageControl1];
}

#pragma mark - topviewDelegate
- (void)singleIconViewTapped:(oldService *)service
{
    [self.delegate whichIconChoosed:service];
}

- (void)changeSubView:(NSArray *)array index:(NSInteger)index
{
    _img_arrow_lead.constant = (SCREEN_WIDTH/4 - 17)/2 + index * SCREEN_WIDTH/4;
    [self initSubTopView:array];
}

#pragma mark - scrollView - delegate
- (void)scrollViewDidScroll:(UIScrollView *)sender{
    
    if (sender == _scrollView1)
    {
        int page = (_scrollView1.contentOffset.x + SCREEN_WIDTH/2)/ (SCREEN_WIDTH);//通过滚动的偏移量来判断目前页面所对应的小白点
        _pageControl1.currentPage = page;//pagecontroll响应值的变化
    }
}
@end
